package com.kitri.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.kitri.db.SetOfDataSource;
import com.kitri.domain.JuctionMbCo;

public class JuctionMbCoDaoImpl implements JuctionMbCoDao {

	@Override
	public void add(JuctionMbCo juctionMbCo) {
		StringBuffer sql = new StringBuffer();

		sql.append("INSERT INTO Juction_Mb_Co ");
		sql.append("VALUES(?, ?)");

		JdbcTemplate jdbcTemplate = new JdbcTemplate(
				SetOfDataSource.getDataSource());
		if (checkIt(juctionMbCo)) {
			jdbcTemplate.update(
					sql.toString(),
					new Object[] { juctionMbCo.getSeqId(),
						juctionMbCo.getCourseId() });
		}
	}


	@Override
	public void remove(JuctionMbCo juctionMbCo) {
		StringBuffer sql = new StringBuffer();

		sql.append("DELETE FROM Juction_Mb_Co ");
		sql.append("WHERE SEQ_ID = ? ");
		sql.append("AND COURSE_ID = ?");

		JdbcTemplate jdbcTemplate = new JdbcTemplate(
				SetOfDataSource.getDataSource());
		jdbcTemplate.update(
				sql.toString(),
				new Object[] {  juctionMbCo.getSeqId(),
					juctionMbCo.getCourseId()});
	}


	@Override
	public void removeOfMember(String seqId) {
		StringBuffer sql = new StringBuffer();

		sql.append("DELETE FROM Juction_Mb_Co ");
		sql.append("WHERE SEQ_ID = ? ");

		JdbcTemplate jdbcTemplate = new JdbcTemplate(
				SetOfDataSource.getDataSource());
		jdbcTemplate.update(
				sql.toString(),
				new Object[] { seqId });
	}


	@Override
	public boolean checkIt(JuctionMbCo juctionMbCo) {
		String sql = "SELECT * FROM Juction_Mb_Co WHERE SEQ_ID = ? AND COURSE_ID = ?";

		final JuctionMbCo juctionObject = new JuctionMbCo();

		JdbcTemplate jdbcTemplate = new JdbcTemplate(
				SetOfDataSource.getDataSource());
		jdbcTemplate.query(sql, new Object[] 
				{ juctionMbCo.getSeqId(), juctionMbCo.getCourseId() }, new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				juctionObject.setSeqId(rs.getString("SEQ_ID"));
				juctionObject.setCourseId(rs.getString("COURSE_ID"));
			}
		});

		if (juctionObject.getSeqId() == null)
			return true;
		return false;
	}


	@Override
	public List<JuctionMbCo> list() {
		// TODO Auto-generated method stub
		return null;
	}
}
